package ims;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.dudu.utils.Encrypt;
import com.dudu.utils.HttpRequest;

public class Transfer {

	//url
	public static final String URL = "http://117.27.129.6:8000/httpIntf/dealIntf";
	//spID
	public static final String SPID = "802964";
	//密码
	public static final String PASSWORD = "heguoping123";
	//原appID
	public static final String APPID_FROM = "279";
	//目的appID
	public static final String APPID_TO = "782";
	
	/**
	 * 计费账号转移
	 * @param ims
	 * @param key
	 * @return
	 */
	public static JSONObject TRANSFER_IMS(String ims, String key){
		System.out.println("-----------TRANSFER_IMS begin");
		JSONObject object = new JSONObject();
		//请求参数
		String methodName = "TRANSFER_IMS";
		//号码类型，8659X开头号码需填写1_1;11位天翼号码需填写1_2
		String type = "1_2"; 
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
		String timestamp = sdf.format(Calendar.getInstance().getTime());
		StringBuffer urlBuffer = new StringBuffer(URL);
		urlBuffer.append("?postData=<Request><Head><MethodName>"+methodName+"</MethodName>");		
		urlBuffer.append("<Spid>").append(SPID).append("</Spid>");
		urlBuffer.append("<Appid>").append(APPID_FROM).append("</Appid>");
		urlBuffer.append("<Passwd>").append(Encrypt.sha1Encrypt(PASSWORD)).append("</Passwd>");
		urlBuffer.append("<Timestamp>").append(timestamp).append("</Timestamp>");
		urlBuffer.append("<Authenticator>").append(Encrypt.sha1Encrypt(timestamp + methodName + SPID + PASSWORD)).append("</Authenticator>");
		urlBuffer.append("</Head><Body>");
		urlBuffer.append("<Ims>").append(ims).append("</Ims>");
		urlBuffer.append("<Key>").append(key).append("</Key>");
		urlBuffer.append("<ToAppid>").append(APPID_TO).append("</ToAppid>");
		urlBuffer.append("<Type>").append(type).append("</Type>");
		urlBuffer.append("</Body></Request>");
				
		System.out.println("-------TRANSFER_IMS url:"+urlBuffer);
		try {
			String data = HttpRequest.getDataThreeTime(urlBuffer.toString());
			System.out.println("-----TRANSFER_IMS response:"+data);
			Document doc = DocumentHelper.parseText(data);
			Element root = doc.getRootElement();
			Element head = root.element("Head");
			Element body = root.element("Body");
			object.put("result", head.elementTextTrim("Result"));
			object.put("discripe", head.elementTextTrim("ResultDesc"));
			object.put("ims", body.elementTextTrim("Ims"));
			object.put("key", body.elementTextTrim("Key"));
		} catch (Exception e) {
			System.out.println("------号码转移接口调用异常！----");
		}
		return object;
	}
	
	public static String order(String ims, String key){
		  
		StringBuffer urlBuffer = new StringBuffer(URL);
		urlBuffer.append("?postData=<Request>");
		urlBuffer.append("<Head><MethodName>IMS_BUY</MethodName>"); 
		urlBuffer.append("<Spid>"+SPID+"</Spid>");
		urlBuffer.append("<Appid>"+APPID_FROM+"</Appid>");
		urlBuffer.append("<Passwd>"+Encrypt.sha1Encrypt(PASSWORD)+"</Passwd>");
		urlBuffer.append("</Head>");
		urlBuffer.append("<Body><PointsSpId>100221</PointsSpId><Ims>"+ims+"</Ims>");
		urlBuffer.append("<Key>"+key+"</Key><Type>1</Type><EffectType>1</EffectType></Body>");
		urlBuffer.append("</Request>");
		System.out.println("定购套餐请求---send time account request:"+urlBuffer);
		String xml = HttpRequest.getData(urlBuffer.toString());		
		System.out.println("定购套餐响应---send time account response:"+xml);
		String result = null;
		try {
			Document doc = DocumentHelper.parseText(xml);
			Element root = doc.getRootElement();
			Element head = root.element("Head");
			result = head.elementTextTrim("Result");
		} catch (DocumentException e) {
			e.printStackTrace();
		}		
		if(result != null && !result.equals("0")){
			System.out.println("定购套餐失败");
		}		
		return result;
	}
	
	public static void main(String[] args){
		//待转移的号码
		Map<String,String> mMaps = new HashMap<String ,String>();
		mMaps.put("8659522579149", "17689702");
		mMaps.put("8659188320455", "58994812");
		mMaps.put("865927268245", "18855492");
		mMaps.put("8659186258458", "04170959");
		/*mMaps.put("13328614545", "69093072");
		mMaps.put("18950907277", "35929471");
		mMaps.put("18060128122", "06316404");
		mMaps.put("13328580611", "15936436");
		mMaps.put("13338223986", "07675325");
		mMaps.put("13328942456", "61779930");
		mMaps.put("13375909998", "54480760");
		mMaps.put("18060248288", "82490850");
		mMaps.put("18060248088", "95313982");*/
		for(String key : mMaps.keySet()){
			/*JSONObject jsonData = TRANSFER_IMS(key, mMaps.get(key));
			order(jsonData.optString("ims"), jsonData.optString("key"));*/
			order(key, mMaps.get(key));
		}

	}
	
}
